package com.newviews.tcmip.web.controller;

import com.newviews.tcmip.common.constant.CommonConsts;
import com.newviews.tcmip.common.model.PageModel;
import com.newviews.tcmip.common.model.ResultModel;
import com.newviews.tcmip.dao.EtcmHerbExMapper;
import com.newviews.tcmip.service.inf.EtcmDiseaseExServiceInf;
import com.newviews.tcmip.service.inf.EtcmHerbExServiceInf;
import com.newviews.tcmip.service.inf.EtcmHerbIngrServiceInf;
import com.newviews.tcmip.web.vo.etcmherb.download.EtcmHerbByIngrExportReqVO;
import com.newviews.tcmip.web.vo.etcmpx.PxIngrStatisticsRespVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 疾病扩展信息 前端控制器
 * </p>
 *
 * @author lxc
 * @since 2024-03-01
 */
@RestController
@RequestMapping("/etcm/admet")
@Api(tags = "admet")
public class EtcmAdmetController {
    @Resource
    private EtcmHerbIngrServiceInf etcmHerbIngrServiceInf;
    @Resource
    private EtcmDiseaseExServiceInf etcmDiseaseExServiceInf;

    /**
     * <p>
     * 获取相关成分
     * </p>
     *
     * @param langCode      语言编码
     * @param uiTypeCode    ui编码
     * @param pageNo        页码
     * @param pageSize      每页条数
     * @param content       关键字
     * @return
     */
    @ApiOperation(value = "admet查询")
    @GetMapping("/page")
    public ResultModel<PageModel<Map<String,String>>> getPageByIngr(@RequestParam @NotBlank(message = "语言编码不能为空") String langCode,
                                                                    @RequestParam @NotBlank(message = "ui编码不能为空") String uiTypeCode,
                                                                    @RequestParam(required = false, defaultValue = "1") Integer pageNo,
                                                                    @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                                    @RequestParam(required = false) String content,
                                                                    @RequestParam(required = false) String ingrBbb,
                                                                    @RequestParam(required = false) String ingrCaco2,
                                                                    @RequestParam(required = false) String ingrHia,
                                                                    @RequestParam(required = false) String ingrHob,
                                                                    @RequestParam(required = false, defaultValue = "desc") String sortType){
        return etcmHerbIngrServiceInf.getPageByIngr(langCode,uiTypeCode,pageNo, pageSize, content,ingrBbb,ingrCaco2,ingrHia,ingrHob,sortType);
    }

    @PostMapping("/download")
    @ApiOperation(value = "admet下载")
    public void downloadHerbIngr(
            @ApiIgnore HttpServletResponse response,
            @RequestBody @Valid EtcmHerbByIngrExportReqVO reqVO) {
        try {
            List<Map<String, String>> list = etcmHerbIngrServiceInf.downloadAdmet(reqVO.getIngrHia(),reqVO.getIngrHob(),reqVO.getIngrBbb(),reqVO.getIngrCaco2(),reqVO.getUiTypeCode(),reqVO.getLangCode(),reqVO.getSortType(),reqVO.getContent());
            etcmDiseaseExServiceInf.getDownload(response, CommonConsts.UI_TYPE_CODE_PX_INGR,reqVO.getLangCode(),reqVO.getExportType(),list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @GetMapping("/statistics")
    @ApiOperation("admet统计")
    public ResultModel<PxIngrStatisticsRespVo> getIngredientStatistics(@RequestParam(required = false) String content,
                                                                       @RequestParam String langCode){

        return etcmHerbIngrServiceInf.getIngredientStatistics(content, langCode);
    }

}
